﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Xml.Linq;
using Framework.Interfaces;
using RSSNewsGrabberModule.Configuration;
using RSSNewsGrabberModule.Mapping;
using RSSNewsGrabberModule.Models;

namespace RSSNewsGrabberModule
{
    public class DataLoader
    {
        public IDataHeader<IDataChild> LoadData(string url,IBaseCriteria criteria,AppSettings configInfo)
        {
            if(criteria==null) throw new NullReferenceException();
            var mappings = MappingLoader.Load(configInfo.FieldConfigName);

            var channel = XDocument.Load(url);
            var mappingKey = url;
            if (!mappings.Mappings.ContainsKey(url))
                mappingKey = configInfo.DefaultMappingKey;
            var parser = new RssParser<RssDataHeader, RssDataItem>(mappings.Mappings[mappingKey],
                                                                   criteria.AdditionalContentEnabled,
                                                                   configInfo.AdditionalContentRoot);
            var result = parser.ParseFromXml(channel);
            
            return result;
        }

       
    }
}
